package com.isolation.harassed.view;

import com.isolation.harassed.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 一个通用的顶部标题面板类，带有左则返回按键、右则显示更多按键及圆形进度条，任何Activity类若希望处理返回和更多按键的点击事件，
 * 只需要在该Activity中实现该内部的 {@link TitlePanel.OnBackListener#OnBackListener
 * OnBackListener}或{@link TitlePanel.OnShowMoreListener#OnShowMoreListener
 * OnShowMoreListener} 接口相应的函数即可。
 * 
 * @author allon
 * @version 1.0.1
 * @date 2016年3月23日
 */
public class TitlePanel extends Fragment implements View.OnClickListener {

	private TextView mTitle = null;
	private ProgressBar mProgressBar = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.global_title_panel, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		this.mTitle = (TextView) getView().findViewById(R.id.global_title_text);
		getView().findViewById(R.id.global_title_more).setOnClickListener(this);
		getView().findViewById(R.id.global_title_back).setOnClickListener(this);
		this.mProgressBar = (ProgressBar) getView().findViewById(
				R.id.global_title_progress);
	}

	/**
	 * 改变{@code TitlePanel}的标题
	 * 
	 * @param title
	 *            标题文字
	 */
	public void setTitle(String title) {
		if (null != mTitle) {
			mTitle.setText(title);
		}
	}

	/**
	 * 设置{@code TitlePanel}的任务进度控件的可见状态
	 * 
	 * @param v
	 *            One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
	 */
	public void setProgressVisibility(int v) {
		if (null != mProgressBar) {
			mProgressBar.setVisibility(v);
		}
	}

	/**
	 * 定义{@code TitlePanel}监听的点击事件{@code onShowMoreContent}
	 * 
	 * @author allon
	 * @version 1.0.1
	 * @date 2016年3月23日
	 */
	public interface OnShowMoreListener {
		/**
		 * 监听到右侧更多按钮被按下事件
		 * 
		 * @param btn
		 *            更多按钮
		 */
		public void onShowMoreContent(View btn);
	}

	/**
	 * 定义{@code TitlePanel}监听的点击事件{@code onBackListener}
	 * 
	 * @author allon
	 * @version 1.0.1
	 * @date 2016年3月23日
	 */
	public interface OnBackListener {
		/**
		 * 监听到返回按钮被按下事件
		 * 
		 * @param btn
		 *            返回按钮
		 */
		public void onBackListener(View btn);
	}

	@Override
	public void onClick(View btn) {
		switch (btn.getId()) {
		case R.id.global_title_back:
			if (getActivity() instanceof OnBackListener)
				((OnBackListener) getActivity()).onBackListener(btn);
			break;
		case R.id.global_title_more:
			if (getActivity() instanceof OnShowMoreListener)
				((OnShowMoreListener) getActivity()).onShowMoreContent(btn);

			// default handler
//			new Handler().postDelayed(showMenu, 200);
			break;
		default:
			break;
		}
	}

	/*private Runnable showMenu = new Runnable() {
		public void run() {
			((MainActivity) getActivity()).menu.showMenu();
		}
	};*/
}
